package com.ydche.service.impl;

import com.ydche.client.KernelClient;
import com.ydche.dao.IInsuranceDao;
import com.ydche.idl_output.*;
import com.ydche.service.*;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InsuranceServiceImpl implements InsuranceService {

    @Autowired
    ICustomerService customerService;
    @Autowired
    ICarInfoService carInfoService;
    @Autowired
    InsQryService insQryService;
    @Autowired
    IOrderService orderService;
    @Autowired
    private IInsuranceDao insuranceDao;

    @Autowired
    public KernelClient kernelClient=null;

    @Override
    public QuoteResultBiz getAccurateQuote(Long userId, QuoteParmBiz parm)  throws TException{
        TProtocol protocol = kernelClient.getConnection();
        IKernelRpcService.Iface client = new IKernelRpcService.Client(protocol);
        AccurateQuoteResp quoteResult;
        try {
            quoteResult = client.AccurateQuote(parm.getParm());
            insQryService.addInsQryLog(userId, parm, quoteResult);
        }finally {
            kernelClient.returnConnection(protocol);
        }
        return null;
    }

    @Override
    public ReinsurResp getReinsurance(Long userId, ReinsurParm parm)  throws TException{
        TProtocol protocol = kernelClient.getConnection();
        IKernelRpcService.Iface client = new IKernelRpcService.Client(protocol);
        ReinsurResp resp;
        try {
            resp = client.Reinsur(parm);
            orderService.addInsuranceOrder(userId, parm, resp);
        }finally {
            kernelClient.returnConnection(protocol);
        }
        return resp;
    }

    @Override
    public InsuranceResp getInsuranceInfo(CarInfoParm parm) throws TException{
        TProtocol protocol = kernelClient.getConnection();
        IKernelRpcService.Iface client = new IKernelRpcService.Client(protocol);
        InsuranceResp resp;
        try {
            resp = client.GetInsuranceInfo(parm);
        }finally {
            kernelClient.returnConnection(protocol);
        }
        return resp;
    }

    @Override
    public RiskResp getRiskList()  throws TException{
        TProtocol protocol = kernelClient.getConnection();
        IKernelRpcService.Iface client = new IKernelRpcService.Client(protocol);
        RiskResp resp;
        try {
            resp = client.GetRiskList();
        }finally {
            kernelClient.returnConnection(protocol);
        }
        return resp;
    }

    @Override
    public SupportInsuranceResp getSupportInsuranceUnit(String applicationID, String provinceCode, String cityCode) throws TException{
        TProtocol protocol = kernelClient.getConnection();
        IKernelRpcService.Iface client = new IKernelRpcService.Client(protocol);
        SupportInsuranceResp resp;
        try {
            resp = client.GetSupportInsuranceUnit(applicationID, provinceCode, cityCode);
        }finally {
            kernelClient.returnConnection(protocol);
        }
        return resp;
    }

    @Override
    public ImgUploadResp ztImageUpload(ZtUploadParm parm)  throws TException{
        TProtocol protocol = kernelClient.getConnection();
        IKernelRpcService.Iface client = new IKernelRpcService.Client(protocol);
        ImgUploadResp resp;
        try {
            resp = client.ZtImageUpload(parm);
        }finally {
            kernelClient.returnConnection(protocol);
        }
        return resp;
    }

    @Override
    public InsuranceUnitInfo getInsuranceUnitInfo(String unitCode) throws TException {
        return null;
    }

    @Override
    public InsuranceTypes getInsuranceType(String code)throws TException {
        List<InsuranceTypes> insuranceTypesList = insuranceDao.getInsuranceTypes();
        for(InsuranceTypes insuranceTypes: insuranceTypesList){
            if(insuranceTypes.getCode().equals(code)){
                return insuranceTypes;
            }
        }
        return null;
    }
}
